<?php

namespace App\Http\Controllers\Admin\Pda;

use App\Exceptions\AppException;
use App\Http\Controllers\Controller;
use App\Models\AdminModel;
use App\Models\cainiao\NotifyModel;
use App\Models\lazada\BigBagModel;
use App\Models\customer\OrderRuleModel;
use App\Models\OrderModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

//入库
class Storage extends Controller
{

    /**
     * Notes:验证大包号
     * User: xiangzi
     * Date: 2024/10/26
     * Time: 下午2:45
     * @return \Illuminate\Http\JsonResponse
     */
    public function check(Request $request)
    {
        $customer_id = $request->input('platform');
        $des_country_id = $request->input('des_country_id', 0);
        $goods_attr = $request->input('goods_attr', 0);
//        $goods_value = $request->input('goods_value', 0);
        $bigBagID = $request->input('bigBagID');

        if(!$bigBagID){
            return $this->error('大包号不能为空');
        }

        $find = BigBagModel::where('bigBagID', $bigBagID)->first();

        if($find){
            if($find->platform != $customer_id){
                return $this->success(['result' => 0, 'msg' => '平台不一致']);
            }

            if($des_country_id && $find->des_country_id != $des_country_id){
                return $this->success(['result' => 0, 'msg' => '目的地不一致']);
            }

            if($goods_attr && $find->cargoType != $goods_attr){
                return $this->success(['result' => 0, 'msg' => '货物属性不一致']);
            }

            if($find->order_id){
                return $this->error('该大包号已入库');
            }
            return $this->success(['result' => 1]);
        }else{
            $model = new BigBagModel();
            $model->bigBagID = $bigBagID;
            $model->customer_id = 13;
            $model->des_country_id = $des_country_id;
            $model->cargoType = $goods_attr;
            $model->is_lack = 1;
            $model->save();

            return $this->success(['result' => 1]);
        }
    }


    /**
     * Notes:平台货入库
     * User: xiangzi
     * Date: 2024/10/26
     * Time: 下午4:02
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request) {
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'platform' => 'required',
            'goods_label' => 'required',
//            'storage_remark' => 'required',
            'place_id' => 'required|integer',
            'bigBagID' => 'required|array'
        ], [
            'platform.required' => '平台不能为空',
            'goods_label.required' => '请填写货物标签',
            'place_id.required' => '请选择库位',
            'bigBagID.required' => '大包号不能为空',
            'bigBagID.array' => '大包号必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $des_country_id = 0;
        foreach ($params['bigBagID'] as $bigBagID){
            $des_country_id_find = BigBagModel::where('bigBagID', $bigBagID)->value('des_country_id');
            if($des_country_id_find){
                $des_country_id = $des_country_id_find;
                break;
            }
        }

        $auth_info = AdminModel::where('id', $auth->id)->first();
        $warehouse_id = $auth_info->staff->warehouse_id ?? 0;

        DB::beginTransaction();
        try{
            $model = new OrderModel();
            $model->sn = $this->get_order_sn($params['platform']);
            $model->customer_id = $params['platform'];
            $model->goods_label = $params['goods_label'];
            $model->place_id = $params['place_id'];
            $model->status = OrderModel::STATUS_STORAGE;
            $model->storage_remark = $params['storage_remark'] ?? '';
            $model->storage_time = date('Y-m-d H:i:s');
            $model->big_pkgs = count($params['bigBagID']) ?? 0;
//            $model->small_pkgs = 0;
            $model->is_platform = 1;
            $model->des_country_id = $des_country_id;
            $model->save();

            $small_pkgs = 0; $storage_weight = 0;
            foreach ($params['bigBagID'] as $bigBagID){
                $find = BigBagModel::where('bigBagID', $bigBagID)->first();
                if($find){
                    $find->order_id = $model->id;
                    $find->warehouse_id = $warehouse_id;
                    $find->save();

                    $small_pkgs += $find->parcelQty;
                    $storage_weight += round($find->bigBagWeight / 1000, 2);
                }else{
                    $model_notify = new BigBagModel();
                    $model_notify->bigBagID = $bigBagID;
                    $model_notify->customer_id = $params['platform'];
                    $model_notify->order_id = $model->id;
                    $model_notify->warehouse_id = $warehouse_id;
                    $model_notify->save();
                }
            }

            $model->small_pkgs = $small_pkgs;
            $model->storage_weight = $storage_weight;
            $model->save();
//            BigBagModel::query()->whereIn('bigBagID', $params['bigBagID'])->update(['order_id' => $model->id, 'warehouse_id' => $warehouse_id]);

            DB::commit();

        }catch (AppException $e){
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();

    }


    public function get_order_sn($customer_id) {
        $rule = OrderRuleModel::where('customer_id', $customer_id)->first();
        if($rule){
            $prefix = $rule->prefix;
            $suffix = $rule->suffix;

            return $prefix . $suffix . date('Ymd') . get_big_order_sn($rule->serial_number);
        }else{
            return '';
        }


    }


}
